package com.javadroid.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.javadroid.smartnovelas.R;
import com.javadroid.view.SlideFragment;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class EmissoraAdapter extends BaseAdapter {
 
	private String[] listEmissora;
    private LayoutInflater layoutInflater;
    private DisplayImageOptions mOptions;
    private ImageLoader mImageLoader;
 
    public EmissoraAdapter(Context context, String[] mSeriesTitles) {
        layoutInflater = LayoutInflater.from(context);
        this.listEmissora = mSeriesTitles;
        
        // Setup ImageLoader
        this.mImageLoader = ImageLoader.getInstance();

        this.mOptions = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

        if (!mImageLoader.isInited())
        {
            ImageLoaderConfiguration mImageLoaderConfiguration = new ImageLoaderConfiguration.Builder(context)
            	.defaultDisplayImageOptions(mOptions)
                .build();
            mImageLoader.init(mImageLoaderConfiguration);
        }
                
    }
 
    @Override
    public int getCount() {
        return listEmissora.length;
    }
 
    @Override
    public Object getItem(int position) {
        return listEmissora[position];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    public View getView(int position, View convertView, ViewGroup parent) {
    	ViewHolder holder;
        
        if (convertView == null) {

          	convertView = layoutInflater.inflate(R.layout.row_emissoras, null);

          	holder = new ViewHolder();
            holder.imagemEmissora = (ImageView) convertView.findViewById(R.id.imageViewEmissora);
            holder.imagemEmissora.setDrawingCacheEnabled(true);
                        
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) SlideFragment.ROW_EMISSORA_LARGURA, (int) SlideFragment.ROW_EMISSORA_TAMANHO);
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            holder.imagemEmissora.setLayoutParams(layoutParams);
            
            
            convertView.setTag(holder);
            
            
            
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        switch (position) {
		case 0:
			String globo = "assets://globo.png";
			mImageLoader.displayImage(globo, holder.imagemEmissora, mOptions);
			break;
		case 1:
			String record = "assets://record.png";
			mImageLoader.displayImage(record, holder.imagemEmissora, mOptions);
			break;
		case 2:
			String sbt = "assets://sbt.png";
			mImageLoader.displayImage(sbt, holder.imagemEmissora, mOptions);
			break;			
		default:
			break;
		}
 
        return convertView;
    }
 
    static class ViewHolder {
    	ImageView imagemEmissora;
    }
 
}